<template>
  <div>
    <!-- 搜索表单 -->
    <el-form
      :model="table.where"
      class="ele-form-search ele-table-tool-default"
      size="small"
      @keyup.enter.native="$refs.table.reload({page:{page:1,limit:10}})"
      @submit.native.prevent
    >
      <el-row :gutter="15">
        <el-col :md="6" :sm="12">
          <el-form-item>
            <el-input v-model="table.where.key" placeholder="请输入用户名" clearable size="small" />
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="12">
          <div class="ele-form-actions">
            <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              size="small"
              @click="$refs.table.reload({page:{page:1,limit:10}})"
            >查询
            </el-button>
            <el-button type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small" @click="add">添加</el-button>
            <el-button type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small" @click="remove()">删除
            </el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <!-- 数据表格 -->
    <ele-data-table
      ref="table"
      :config="table"
      :choose.sync="choose"
      height="calc(100vh - 300px)"
      highlight-current-row
    >
      <el-table-column
        prop="name"
        label="姓名"
        show-overflow-tooltip
        min-width="110"
      />
      <el-table-column
        prop="mail"
        label="联系方式"
        show-overflow-tooltip
        min-width="110"
      />
      <el-table-column
        prop="dep"
        label="所属部门"
        show-overflow-tooltip
        min-width="110"
      >
        <template slot-scope="{ row }">
          {{ row.dep === 'None' ? '暂无':row.dep.name }}</template>
      </el-table-column>
      <el-table-column
        prop="is_valid"
        label="是否在职"
        show-overflow-tooltip
        min-width="110"
      >
        <template slot-scope="{ row }">
          <ele-dot
            :type="row.is_valid === 'True' ? 'success':'danger'"
            :text="row.is_valid === 'True' ? '在职':'离职'"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150px" align="center" :resizable="false">
        <template slot-scope="{row}">
          <el-link icon="el-icon-edit" type="primary" :underline="false" @click="edit(row)">修改</el-link>
          <!-- <el-popconfirm title="确定要删除此用户吗？" class="ele-action" @onConfirm="remove(row)">
            <el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false">删除</el-link>
          </el-popconfirm> -->
        </template>
      </el-table-column>
    </ele-data-table>
    <!-- 编辑弹窗 -->
    <el-dialog
      :title="editForm.id?'修改用户':'添加用户'"
      :visible.sync="showEdit"
      width="600px"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      custom-class="ele-dialog-form"
      :lock-scroll="false"
      @closed="editForm={}"
    >
      <el-form ref="editForm" :model="editForm" :rules="editRules" label-width="82px">
        <el-form-item label="员工姓名:" prop="name">
          <el-input
            v-model="editForm.name"
            placeholder="请输入员工姓名"
            clearable
          />
        </el-form-item>
        <el-form-item label="联系方式:" prop="mail">
          <el-input
            v-model="editForm.mail"
            placeholder="请输入联系方式"
            clearable
          />
        </el-form-item>
        <el-form-item label="是否在职">
          <el-switch
            v-model="editForm.is_valid"
            active-value="True"
            inactive-value="False"
          />
        </el-form-item>
        <el-form-item label="所属部门:" prop="dep">
          <treeselect
            v-model="editForm.dep"
            :options="organizationList"
            placeholder="请选择所属部门"
            :default-expand-level="4"
            :normalizer="(d)=>{return { id: d.id, label: d.name }}"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showEdit=false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect' // 下拉树
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { creatPerson, updatePerson } from '@/api/base'
export default {
  name: 'SysUserList',
  components: { Treeselect },
  props: {
    organizationId: {
      type: String,
      default: '0'
    }, // 机构id
    organizationList: {
      type: Array,
      default: () => []
    }, // 全部机构
    departmentName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      table: { url: '/base/list/person/', where: { }}, // 表格配置
      choose: [], // 表格选中数据
      showEdit: false, // 是否显示表单弹窗
      editForm: {
        is_valid: 'True'
      }, // 表单数据
      editRules: { // 表单验证规则
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        mail: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    /* 监听机构id变化 */
    organizationId() {
      this.table.where.dep = this.organizationId
      this.$refs.table.reload({ page: { page: 1, limit: 10 }})
    }
  },
  methods: {
    /* 显示添加 */
    add() {
      this.editForm.dep = this.organizationId
      this.showEdit = true
    },
    /* 显示编辑 */
    edit(row) {
      this.editForm = Object.assign({}, {
        name: row.name,
        is_valid: row.is_valid,
        dep: row.dep.id,
        mail: row.mail,
        id: row.id
      })
      this.showEdit = true
    },
    /* 保存编辑 */
    save() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          this.editLoading = true
          if (this.editForm.id) {
            updatePerson(this.editForm).then(res => {
              const resp = res.data
              if (resp.code === 0) {
                this.$message({ type: 'success', message: '更新成功' })
                this.showEdit = false
                this.$refs.table.reload()
              } else {
                this.$message({ type: 'error', message: resp.msg })
              }
            })
          } else {
            creatPerson(this.editForm).then(res => {
              const resp = res.data
              if (resp.code === 0) {
                this.$message({ type: 'success', message: '保存成功' })
                this.showEdit = false
                this.$refs.table.reload()
              } else {
                this.$message({ type: 'error', message: resp.msg })
              }
            })
          }
          this.editLoading = false
        } else {
          return false
        }
      })
    },
    /* 删除 */
    remove(row) {
      if (!row) { // 批量删除
        if (this.choose.length === 0) return this.$message.error('请至少选择一条数据')
        const ids = this.choose.map(d => d.userId)
        this.$confirm('确定要删除选中的用户吗?', '提示', { type: 'warning' }).then(() => {
          const loading = this.$loading({ lock: true })
          this.$http.delete('/sys/user/batch', { data: ids }).then(res => {
            loading.close()
            if (res.data.code === 0) {
              this.$message({ type: 'success', message: res.data.msg })
              this.$refs.table.reload()
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(e => {
            loading.close()
            this.$message.error(e.message)
          })
        }).catch(() => 0)
      } else { // 单个删除
        const loading = this.$loading({ lock: true })
        this.$http.delete('/sys/user/' + row.userId).then(res => {
          loading.close()
          if (res.data.code === 0) {
            this.$message({ type: 'success', message: res.data.msg })
            this.$refs.table.reload()
          } else {
            this.$message.error(res.data.msg)
          }
        }).catch(e => {
          loading.close()
          this.$message.error(e.message)
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
